package thread;

/**
 * 并发安全的API
 * StringBuilder不是并发安全的，StringBuffer是并发安全的。
 * 多线程场景下为保证数据安全，应当使用StringBuffer。
 */
public class SyncapiDemo {
    public static void main(String[] args) {
        //StringBuilder sb = new StringBuilder();
        StringBuffer sb = new StringBuffer();
        Thread t1 = new Thread(){
            public void run(){
                for (int i = 0; i <1000 ; i++) {
                    sb.append("abc");
                }
            }
        };
        Thread t2 = new Thread(){
            public void run(){
                for (int i = 0; i <1000 ; i++) {
                    sb.append("def");
                }
            }
        };
        t1.start();
        t2.start();

        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
        }
        System.out.println(sb.length());
    }

    }

